package com.zxl.always.on.display.configs.sdk;

import android.content.Context;

import com.zxl.always.on.display.ApplicationHelper;
import com.zxl.always.on.display.tools.AppUtils;
import com.zxl.always.on.display.tools.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Administrator on 2018/2/13.
 */

public class ConfigDataHelper {

    public static final String URL = "http://oc.umeng.com/v2/check_config_update";

    private static String getReuqestParams(String appkey, String channel) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", "online_config");
            jsonObject.put("appkey", appkey);
            Context context = ApplicationHelper.getContext();
            String packageName = context.getPackageName();
            jsonObject.put("version_code", AppUtils.getVersionCodeByPkgName(context, packageName));
            jsonObject.put("package", packageName);
            jsonObject.put("sdk_version", "1.0.0");
            jsonObject.put("idmd5", MD5(packageName));
            jsonObject.put("channel", channel);
            jsonObject.put("last_config_time", 0);
            return jsonObject.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static byte[] doPost(String appkey, String channel) {
        return HttpUtils.doPost(URL, getReuqestParams(appkey, channel));
    }


    public static String MD5(String var0) {
        try {
            MessageDigest var1 = MessageDigest.getInstance("MD5");
            var1.update(var0.getBytes());
            byte[] var2 = var1.digest();
            StringBuffer var3 = new StringBuffer();
            for(int var4 = 0; var4 < var2.length; ++var4) {
                int var5 = 255 & var2[var4];
                var3.append(Integer.toHexString(var5));
            }

            return var3.toString();
        } catch (NoSuchAlgorithmException var6) {
            return "";
        }
    }


}
